<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\base\model;
use think\Model;
use think\Db;
use think\Request;
/**
* 基础方法
* @param  $this->page_size 一页显示几条
* @param  $this->page_number 请求第几条
* @param  $this->condition 条件
* @param  $this->field 查询字段
* @param  $this->postdata 更新数据，新增数据
* @author zje
* @return mixed
*/
class Base extends Model
{
    function __construct(){
        parent::__construct();
        $request = Request::instance();
        $action = $request->action();
        switch ($action) {
            case 'select':
                $this->condition = $this->vdong('condition');
                $this->field = $request->header('field');
                break;
            case 'query':
                $this->condition = $this->vdong('condition');
                $this->field = $request->header('field');

                break;
            case 'insert':
                $this->postdata = $this->vdong('data');
                break;
            case 'update':
                $this->condition = $this->vdong('condition');
                $this->postdata = $this->vdong('data');
                break;
            case 'delete':
                $this->condition = $this->vdong('condition');
                break;
            default:
                break;
        }
    }
    /**
     * 获取多条数据
     * @param string $table  传去掉表前缀的表名
     * @param array  $where  查询条件
     * @param string $field  查询的字段
     * @param string $order  排序
     * @param int    $page_number 页数
     * @param int    $page_size  页面大小
     */
    public function getListByWhere(){
        // 分页
        $page = $this->vdongPage();
        $this->page_size =$page['size'];
        $this->page_number = $page['page_number'];
        $config = array(
            'page' => $this->page_number
            );
        $result = $this->table
            ->field($this->field)
            ->where($this->condition)
            ->order(input('order'))
            ->paginate($this->page_size,false,$config)->toArray();
        return $result;
    }
    /**
     * 获取单条数据
     * @param type $table  传去掉表前缀的表名
     * @param type $where  查询条件
     * @param type $field  查询的字段
     */
    public function getOneByWhere(){
        $result = $this->table
            ->field($this->field)
            ->where($this->condition)
            ->find();
        return $result;
    }
    /**
     * 新增数据
     * @param string $table
     * @param array  $data  新增的数据
     */
    public function addData(){
        $result = $this->table->insert($this->postdata);
        return $result;
    }
    /**
     * 更新数据
     * @param string $table
     * @param array  $where 条件
     * @param array  $data  更新的数据
     */
    public function saveData(){
        $result = $this->table
            ->where($this->condition)
            ->update($this->postdata);
        return $result;
    }
    /**
     * 删除一条数据
     * @param $table 表名
     * @param $where 条件
     * @return int
     */
    public function deleteOne()
    {
       $result=$this->table
           ->where($this->condition)
           ->delete();
        return $result;
    }
    /**
     * 对象转换
     * @param $name 对象名
     * @return array
     */
    function vdong($name) {
        $param = input();
        $array = $param[$name];
      //   $object=json_decode($param[$name]);
      // if (is_object($object)) {
      //   foreach ($object as $key => $value) {
      //     $array[$key] = $value
      //     ;
      //   }
      // }
      // else {
      //   $array = $object;
      // }
      return $array;
    }
    /**
     * 对象转换
     * @return array
     */
     function vdongPage()
    {
        $param = input();
        $array = $param['page'];
      //   $object=json_decode($param['page']);
      //  if (is_object($object)) {
      //   foreach ($object as $key => $value) {
      //     $array[$key] = $value;
      //   }
      // }
      // else {
      //   $array = $object;
      // }
      return $array;
    }
}